package ym.controller.user;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.User;
import ym.service.UserService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台用户接口
 */
@CrossOrigin
@RequestMapping("/back/user")
@RestController
public class UserCon {
    @DubboReference
    private UserService userService;

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/user')")
    public BaseResult add(User user) {
        userService.add(user);
        return BaseResult.ok();
    }

    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(User user) {
        userService.update(user);
        return BaseResult.ok();
    }

    /**
     * 删除用户
     *
     * @param userId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer userId) {
        userService.delete(userId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询用户
     *
     * @param userId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer userId) {
        User user = userService.findById(userId);
        return BaseResult.ok(user);
    }

    /**
     * 分页查询用户
     *
     * @param page
     * @param size
     * @return
     */

    @GetMapping("/search")
    @PreAuthorize("hasAnyAuthority('/user')")
    public BaseResult search(int page, int size) {
        Page<User> brandPage = userService.search(page, size);
        return BaseResult.ok(brandPage);
    }

    /**
     * 查询所有用户
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<User> all = userService.findAll();
        return BaseResult.ok(all);
    }

    /**
     * 修改状态
     *
     * @param userId
     * @return
     */
    @PostMapping("/updateStatus")
    public BaseResult updateStatus(Integer userId) {
        userService.updateStatus(userId);
        return BaseResult.ok();
    }
}
